package reportehorario.vista;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import reportehorario.controlador.ControladorEntrada;
import reportehorario.controlador.ControladorHorario;
import reportehorario.controlador.ControladorUsuario;
import reportehorario.recursos.beans.BeanEntradaXUsuarioXFecha;
import reportehorario.recursos.beans.BeanJustificaciones;
import reportehorario.recursos.beans.BeanRecord;
import reportehorario.recursos.beans.BeanUsuario;

/**
 *
 * @author Metis Gaia 2010-02
 */
public class PanelReporteTodos extends javax.swing.JPanel {

    ControladorHorario controladorHorario = new ControladorHorario();
    ControladorUsuario controladorUsuario = new ControladorUsuario();
    ControladorEntrada controladorEntrada = new ControladorEntrada();
    ArrayList<BeanUsuario> listaUsuarios = null;
    ArrayList<BeanJustificaciones> listaJustificacionesXFila = null;
    String cadenaDiasJustificacion = "Los días son:";

    /** Creates new form reporteTodos */
    public PanelReporteTodos() {
        try {
            listaUsuarios = controladorUsuario.obtenerUsuarios();
        } catch (Exception ex) {
            Logger.getLogger(Reporte.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblHorario = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        pnBusqueda = new javax.swing.JPanel();
        btnConsultar = new javax.swing.JButton();
        ccDesde = new org.freixas.jcalendar.JCalendarCombo();
        ccHasta = new org.freixas.jcalendar.JCalendarCombo();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(680, 480));

        tblHorario.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "NOMBRE", "MINUTOS TARDE", "CANT. DIAS", "PROM. MIN. DE TOLERARANCIA USADOS", "DIAS MARCADOS", "DIAS JUSTIFICADOS"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblHorario.getTableHeader().setReorderingAllowed(false);
        tblHorario.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblHorarioMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblHorario);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Todos los Usuarios");

        pnBusqueda.setBorder(javax.swing.BorderFactory.createTitledBorder("Busqueda"));

        btnConsultar.setText("Consultar");
        btnConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarActionPerformed(evt);
            }
        });

        jLabel3.setText("Desde:");

        jLabel4.setText("Hasta:");

        javax.swing.GroupLayout pnBusquedaLayout = new javax.swing.GroupLayout(pnBusqueda);
        pnBusqueda.setLayout(pnBusquedaLayout);
        pnBusquedaLayout.setHorizontalGroup(
            pnBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnBusquedaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnConsultar)
                    .addGroup(pnBusquedaLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(ccDesde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, Short.MAX_VALUE)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ccHasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(23, 23, 23))
        );
        pnBusquedaLayout.setVerticalGroup(
            pnBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnBusquedaLayout.createSequentialGroup()
                .addContainerGap(32, Short.MAX_VALUE)
                .addGroup(pnBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(ccHasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(ccDesde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnConsultar))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(278, 278, 278)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(pnBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 586, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(47, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(35, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
        try {
            // TODO add your handling code here:
            ArrayList<BeanRecord> listaBeanRecord = new ArrayList<BeanRecord>();
            java.sql.Date fechaDesde = new java.sql.Date(ccDesde.getDate().getTime());
            java.sql.Date fechaHasta = new java.sql.Date(ccHasta.getDate().getTime());
            //idUsuario ponemos igual a cero (0), para que haga una consulta de todos los usuarios en ese rango de fecha
            ArrayList<BeanEntradaXUsuarioXFecha> listaEntradas = controladorEntrada.obtenerEntradas(0, fechaDesde, fechaHasta);

            for (int i = 0; i < listaUsuarios.size(); i++) {
                if (!listaEntradas.isEmpty()) {
                    listaBeanRecord.add(controladorHorario.obtenerRecordXUsuarioXRangoFecha(listaUsuarios.get(i), listaEntradas));
                } else {
                    System.out.println("lista de entrada para este usuario esta vacia...");
                }
            }

            int contador = 0;
            DefaultTableModel datos = (DefaultTableModel) tblHorario.getModel();
            datos.setRowCount(0);
            Object[] rowData;

            listaJustificacionesXFila = new ArrayList<BeanJustificaciones>();

            for (BeanRecord x : listaBeanRecord) {
                if (x.getJustificaciones() != null) {
                    if (x.getJustificaciones().getJustificacion().compareTo("") != 0) {
                        x.getJustificaciones().setIndice_tabla(contador);
                        listaJustificacionesXFila.add(x.getJustificaciones());
                    }
                }
                contador++;
                rowData = new Object[]{
                            x.getUsuario(),
                            x.getMin_tardanza(),
                            x.getDias_tardanza(),
                            x.getProm_minutos_usados(),
                            x.getDias_marcados(),
                            x.getDias_justificados()
                        };
                datos.addRow(rowData);

            }
        } catch (Exception ex) {
            Logger.getLogger(Reporte.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnConsultarActionPerformed

    private void tblHorarioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblHorarioMouseClicked
        // TODO add your handling code here:
        cadenaDiasJustificacion = "Los días son:";

        if (tblHorario.getSelectedColumn() == 5) {
            if ((Integer) tblHorario.getValueAt(tblHorario.getSelectedRow(), tblHorario.getSelectedColumn()) != 0) {
                System.out.println(listaJustificacionesXFila.size());
                for (BeanJustificaciones bj : listaJustificacionesXFila) {
                    if (bj.getIndice_tabla() == tblHorario.getSelectedRow()) {
                        cadenaDiasJustificacion += bj.getJustificacion();
                    }
                }
                //cadenaDiasJustificacion += listaJustificacionesXFila.get(tblHorario.getSelectedRow()).getJustificacion().toString();
                System.out.println(cadenaDiasJustificacion);
                JOptionPane.showMessageDialog(null, cadenaDiasJustificacion, "Dias Justificados", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }//GEN-LAST:event_tblHorarioMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConsultar;
    private org.freixas.jcalendar.JCalendarCombo ccDesde;
    private org.freixas.jcalendar.JCalendarCombo ccHasta;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnBusqueda;
    private javax.swing.JTable tblHorario;
    // End of variables declaration//GEN-END:variables
}
